package com.quanyan.stadium.biz.redis.impl;

import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.stadium.biz.redis.UserGameScheduleCacheDao;
import com.quanyan.stadium.component.cache.MyRedisTemplate;
import com.quanyan.stadium.component.cache.StadiumRedisConstants;
import com.quanyan.stadium.entity.vo.GameSchedule;
import com.quanyan.stadium.entity.vo.PKEventVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by lei.w on 2016/7/15.
 */
@Service
public class UserGameScheduleCacheDaoImpl implements UserGameScheduleCacheDao {

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    private final static int EXPIRE_TIME = 24*60*60-10;

    @Override
    public void putUserGameRecordSet(Integer uid, Map<byte[], Double> scoreMembers) {
        if (scoreMembers.isEmpty()) {
            return;
        }

        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.userGameRecord(uid));
        myRedisTemplate.zadd(redisKey,scoreMembers);
        myRedisTemplate.expire(redisKey, EXPIRE_TIME);
    }

    @Override
    public Set<byte[]> queryUserGameRecordSetInterval(Integer uid, Integer min, Integer max) {
        Set<byte[]> userGameRecordSet = Collections.emptySet();
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.userGameRecord(uid));
        if (myRedisTemplate.exists(redisKey)) {
            return myRedisTemplate.zrange(redisKey, min, max);
        }

        return userGameRecordSet;
    }

    @Override
    public void delUserGameRecordSetInterval(Integer uid) {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.userGameRecord(uid));
        myRedisTemplate.delteKey(redisKey);
    }

    @Override
    public void putPkRecord(PKEventVo pkEventVo) {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.pkInfoKey(pkEventVo.getId()));
        myRedisTemplate.set(redisKey, pkEventVo);
        myRedisTemplate.expire(redisKey, EXPIRE_TIME);
    }

    @Override
    public PKEventVo getUserPkRecord(Integer pkid) {
        PKEventVo pkEventVo = null;
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.pkInfoKey(pkid));
        Object o = myRedisTemplate.get(redisKey);
        if (o != null) {
            pkEventVo = (PKEventVo)o;
        }
        return pkEventVo;
    }

    @Override
    public void putUserGameSchedule(GameSchedule gameSchedule) {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.userGameScheduleKey(gameSchedule.getId()));
        myRedisTemplate.set(redisKey, gameSchedule);
        myRedisTemplate.expire(redisKey, EXPIRE_TIME);
    }

    @Override
    public GameSchedule getUserGameSchedule(Integer id) {
        GameSchedule gameSchedule = null;
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.userGameScheduleKey(id));
        Object o = myRedisTemplate.get(redisKey);
        if (o != null) {
            gameSchedule = (GameSchedule)o;
        }

        return gameSchedule;
    }

    @Override
    public boolean isUserPkScoreEntryFlag(Integer uid) {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.userPKScoreEntryFlagKey(uid));
        Object o = myRedisTemplate.get(redisKey);
        if (o != null) {
            return Calendar.getInstance().get(Calendar.DAY_OF_MONTH) == ((Integer) o).intValue();
        }

        return false;
    }

    @Override
    public void setUserPkScoreEntryFlag(Integer uid) {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.userPKScoreEntryFlagKey(uid));
        myRedisTemplate.set(redisKey, Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
        myRedisTemplate.expire(redisKey, EXPIRE_TIME);
    }

}
